#include <pthread.h>
#include <unistd.h>
#include <iostream>

int flag = 100;

void printTid(pthread_t& tid)
{
    printf("0x%lx\n", tid);
}

std::string FormatId(const pthread_t& tid)
{
    char id[64];
    snprintf(id, sizeof(id), "0x%lx", tid);
    return id;
}


void* routine(void* args)
{
    std::string name = static_cast<const char*>(args);
    int cnt = 5;
    while(cnt--)
    {
        std::cout << "我是一个新线程，我的name:" << name << ",我的tid: " << FormatId(pthread_self()) << std::endl;
        flag++;
        sleep(1);
    }

    return (void*)123;
}


int main()
{
    pthread_t tid = 0;
    int n = pthread_create(&tid, nullptr, routine, (void*)"thread-1");
    std::cout << "输出型参数tid:";
    printTid(tid);
    int cnt = 5;
    while(cnt--)
    {
        std::cout << "我是主线程，我的name:thread-main，我的pid:" << FormatId(pthread_self());
        std::cout << "，flag:" << flag << std::endl;
        sleep(2);
    }


    void* ret = nullptr;
    pthread_join(tid,&ret);
    std::cout << "返回值是：" << (long long int)ret << std::endl;
    return 0;
}
